//
//  SuitView.m
//  PurchasingMeeting
//
//  Created by zhuang yihang on 7/16/13.
//  Copyright (c) 2013 YX. All rights reserved.
//

#import "SuitView.h"
#import "SuitObject.h"
#import "UIHelper.h"
#import "AppManager.h"
#import "DataManager.h"
#import "ProductObject.h"
#import "PMGlobal.h"

#import "TKImageCache.h"
#import "ProductThumbView.h"

@interface SuitView(){
    SuitObject *suit_;
    
    UIImageView *suitImageView_;
    
    TKImageCache *imageCache_;
}

@end

@implementation SuitView
@synthesize suitImageView = suitImageView_;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.showsVerticalScrollIndicator = NO;
    }
    return self;
}

- (void)dealloc{
    [imageCache_ cancelOperations];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (id)initWithSuitObject:(SuitObject*)object frame:(CGRect)frame{
    suit_ = object;
    
    
    imageCache_ = [[TKImageCache alloc] initWithCacheDirectoryName:@"image"];
    imageCache_.notificationName = @"suit_image";
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(suit_image:) name:@"suit_image" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(numreload:) name:@"numreload" object:nil];

    
#ifdef DHH_iOS
    UIImage *img = [[UIHelper getInstance] getBundleImage:@"suitView_bg.png"];
    
    img = [img resizableImageWithCapInsets:UIEdgeInsetsMake(16, 16, 16, 16)];
    
    self = [self initWithFrame:frame];
#else
    UIImage *img = [[UIHelper getInstance] getBundleImage:@"suitView_bg.png"];
    
    img = [img resizableImageWithCapInsets:UIEdgeInsetsMake(16, 16, 16, 16)];
    
    self = [self initWithFrame:CGRectMake(0, 0, 920, 594)];
#endif
    if (self) {
#ifdef DHH_iOS
         UIImageView *imgV = [[UIHelper getInstance] createViewByImage:img];
         imgV.frame = self.bounds;
         imgV.backgroundColor = [UIColor clearColor];
         [self addSubview:imgV];
         
        UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 260, 340)];
        imageview.centerX = self.width/2;
        imageview.backgroundColor = [UIColor grayColor];
        [self addSubview:imageview];
        
        suitImageView_ = imageview;
        suitImageView_.contentMode = UIViewContentModeScaleAspectFill;
        suitImageView_.clipsToBounds = YES;
        
        NSString *url = [NSString stringWithFormat:@"_%@",suit_.pic_path];
        NSString *path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:url];
        [imageCache_ imageForKey:suit_.pic_path url:[NSURL URLWithString:path] queueIfNeeded:YES tag:99+100*suit_.n_id];
        suitImageView_.userInteractionEnabled = YES;
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click)];
        [suitImageView_ addGestureRecognizer:gesture];
        
        for (int i = 0; i < suit_.components.count; i++) {
            UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 60)];
            [self addSubview:v];
            v.tag = 10 + i;
            //            v.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
            v.backgroundColor = [UIColor clearColor];
            v.left = imageview.left;
            v.top = imageview.bottom + 10 + 70 * i;
            [self addTapGesture:v];
            v.userInteractionEnabled = YES;
            
            UIView *vv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 60)];
            [v addSubview:vv];
          //  vv.left = imageview.right + 10;
          //  vv.top = 10 + 90 * i;
            vv.layer.borderColor = [UIColor colorWithRed:0 green:192/255.0 blue:1 alpha:1].CGColor;
            vv.layer.borderWidth = 2;
            vv.tag = 500;

            UIImageView *pImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 60)];
            pImageView.userInteractionEnabled = YES;
            [vv addSubview:pImageView];
           // pImageView.left = imageview.right + 10;
           // pImageView.top = 10 + 90 * i;
            pImageView.tag = 1000;
            
            SubSuitObject *obj = [suit_.components objectAtIndex:i];
            ProductObject *pObj = [[DataManager sharedManager] getProduct:obj.productCode withColorID:obj.colorid];
            
            UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 15)];
            l.backgroundColor = [UIColor clearColor];
            l.font = [UIFont systemFontOfSize:14];
            [v addSubview:l];
            l.textColor = [UIColor blackColor];
            l.center = CGPointMake(0, 10);
            l.left = pImageView.right + 10;
            l.textAlignment = UITextAlignmentLeft;
            l.text = [NSString stringWithFormat:@"%@",obj.subSuitType];
            
            UILabel *codeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 170, 15)];
            codeLabel.backgroundColor = [UIColor clearColor];
            codeLabel.font = [UIFont systemFontOfSize:12];
            [v addSubview:codeLabel];
            codeLabel.textColor = [UIColor blackColor];
            codeLabel.top = l.bottom+5;
            codeLabel.left = l.left;
            codeLabel.textAlignment = UITextAlignmentLeft;
            if (pObj && obj.productCode && obj.productCode.length>0) {
                NSString *str = [NSString stringWithFormat:@"型号: %@  颜色: %@%@",obj.productCode,pObj.pColorCode,pObj.pColor];
                codeLabel.text = str;
            }else{
                codeLabel.text = @"暂无";
            }
            codeLabel.adjustsFontSizeToFitWidth = YES;
            
            UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 15)];
            priceLabel.backgroundColor = [UIColor clearColor];
            priceLabel.font = [UIFont systemFontOfSize:12];
            [v addSubview:priceLabel];
            priceLabel.textColor = [UIColor blackColor];
            priceLabel.top = codeLabel.bottom+5;
            priceLabel.left = l.left;
            priceLabel.textAlignment = UITextAlignmentLeft;
            if (pObj && obj.productCode && obj.productCode.length>0) {
                priceLabel.text = [NSString stringWithFormat:@"价格: ￥%.0f",pObj.pSale];
            }else{
                priceLabel.text = @"暂无";
            }
            
            ShopObject *shopobj=[[DataManager sharedManager]getCurrentShop];
            int orderNumber = [[DataManager sharedManager]getOrderAmount:pObj Shopid:shopobj.shopID];
            NSString *num = [NSString stringWithFormat:@"已下订单 : %d",orderNumber];
            UILabel *count = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 15)];
            count.centerY = l.centerY;
            count.left = l.right;
            count.font = [UIFont systemFontOfSize:12];
            count.text = num;
            count.tag = 183000+i;
            [v addSubview:count];
            
            UIView *tmpSep = [[UIView alloc] initWithFrame:CGRectMake(0, 0, imageview.width, 1)];
            tmpSep.backgroundColor = [UIColor colorWithRed:158/255.0 green:121/255.0 blue:87/255 alpha:1];
            tmpSep.top = pImageView.bottom+5;
            tmpSep.left = 0;
            [v addSubview:tmpSep];
            // UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
           // [pImageView addGestureRecognizer:tap];
            self.contentSize = CGSizeMake(300, imageview.bottom+40+70*(i+1));
        }
        
#else
        UIImageView *imgV = [[UIHelper getInstance] createViewByImage:img];
        imgV.frame = self.bounds;
        imgV.backgroundColor = [UIColor clearColor];
        [self addSubview:imgV];
        
        UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(30, 40, 300, 510)];
        imageview.backgroundColor = [UIColor grayColor];

        imageview.tag = 1000;
        [self addSubview:imageview];
        
        suitImageView_ = imageview;
        suitImageView_.contentMode = UIViewContentModeScaleAspectFit;
        suitImageView_.clipsToBounds = YES;
        
        NSString *url = [NSString stringWithFormat:@"_%@",suit_.pic_path];
        NSString *path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:url];
        [imageCache_ imageForKey:suit_.pic_path url:[NSURL URLWithString:path] queueIfNeeded:YES tag:99+100*suit_.n_id];
        suitImageView_.userInteractionEnabled = YES;
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click)];
        [suitImageView_ addGestureRecognizer:gesture];
        
        float interval = imageview.height/(suit_.components.count);
        
        if ([[[PMGlobal getInstance]gettype] isEqualToString:@"type1"]) {
            
            
            UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 650, 500)];
            [self addSubview:scroll];
            scroll.top = suitImageView_.top;
            scroll.left = suitImageView_.right + 25;
            
            for (int i = 0; i < suit_.components.count; i++) {
                /*
                UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 530, interval)];
                [self addSubview:v];
                v.tag = 10 + i;
                //            v.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
                v.backgroundColor = [UIColor clearColor];
                v.left = sep.right + 25;
                v.top = imageview.top + interval*i;
                
                //            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
                //            [v addGestureRecognizer:tap];
                [self addTapGesture:v];
                v.userInteractionEnabled = YES;
                */
                
                int row = i / 2;
                int col = i % 2;
                
                SubSuitObject *obj = [suit_.components objectAtIndex:i];
                ProductObject *pObj = [[DataManager sharedManager] getProductEx:obj.productCode withColorID:obj.colorid];
                
                ProductThumbView *v = [[ProductThumbView alloc] initWithProduct:pObj tag:10];
                v.tag = 1001+i;
                NSString *s = [[PMGlobal getInstance] getImageURL];
                NSString *path = [s stringByAppendingPathComponent:pObj.pThumbURL];
                path = [path stringByReplacingOccurrencesOfString:@"http:/" withString:@"http://"];
                UIImage *img = [imageCache_ imageForKey:pObj.pThumbURL url:[NSURL URLWithString:path] queueIfNeeded:YES tag:i+100*suit_.n_id];
                
                if (img==nil || ![[PMGlobal getInstance]isImageEnable]) {
                    img = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];;
                }
                v.imageview.image = img;
                
                [scroll addSubview:v];
                
                v.left = 50+(v.width+50)*col;
                v.top = (v.height+45)*row;
                
                scroll.contentSize = CGSizeMake(scroll.width, v.bottom);

                /*
                UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 30)];
                l.backgroundColor = [UIColor clearColor];
                l.font = [UIFont systemFontOfSize:24];
                [v addSubview:l];
                l.textColor = [UIColor blackColor];
                l.center = CGPointMake(0, v.height/2);
                l.left = 0;
                l.text = [NSString stringWithFormat:@"%@:",obj.subSuitType];
                
                ProductObject *pObj = [[DataManager sharedManager] getProductEx:obj.productCode withColorID:obj.colorid];
                UIView *tmpSep = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270, 2)];
                tmpSep.backgroundColor = [UIColor colorWithRed:158/255.0 green:121/255.0 blue:87/255 alpha:1];
                tmpSep.center = l.center;
                tmpSep.left = l.right-10;
                [v addSubview:tmpSep];
                
                UILabel *codeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 170, 30)];
                codeLabel.backgroundColor = [UIColor clearColor];
                codeLabel.font = [UIFont systemFontOfSize:22];
                [v addSubview:codeLabel];
                codeLabel.textColor = [UIColor blackColor];
                codeLabel.bottom = tmpSep.top;
                codeLabel.left = tmpSep.left-10;
                codeLabel.textAlignment = UITextAlignmentCenter;
                if (pObj && obj.productCode && obj.productCode.length>0) {
                    NSString *str = [NSString stringWithFormat:@"%@-%@",obj.productCode,pObj.pColor];
                    codeLabel.text = str;
                }else{
                    codeLabel.text = @"暂无";
                }
                codeLabel.adjustsFontSizeToFitWidth = YES;
                
                UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 30)];
                priceLabel.backgroundColor = [UIColor clearColor];
                priceLabel.font = [UIFont systemFontOfSize:22];
                [v addSubview:priceLabel];
                priceLabel.textColor = [UIColor blackColor];
                priceLabel.top = codeLabel.top;
                priceLabel.left = codeLabel.right+20;
                priceLabel.textAlignment = UITextAlignmentCenter;
                if (pObj && obj.productCode && obj.productCode.length>0) {
                    priceLabel.text = [NSString stringWithFormat:@"￥%.0f",pObj.pSale];
                }else{
                    priceLabel.text = @"暂无";
                }
                
                
                UIButton *other = [UIButton buttonWithType:UIButtonTypeCustom];
                other.frame = CGRectMake(0, 0, 50, 30);
                other.center = CGPointMake(680, 90);
                other.top = tmpSep.bottom + 50+interval*i;
                [other setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"anniu.png"] forState:UIControlStateNormal];
                [other setTitle:@"其他" forState:UIControlStateNormal];
                [other addTarget:self action:@selector(other:) forControlEvents:UIControlEventTouchUpInside];
                other.tag = 182000+i;
                [self addSubview:other];
                
                other.hidden = YES;
                
                ShopObject *shopobj=[[DataManager sharedManager]getCurrentShop];
                int orderNumber = [[DataManager sharedManager]getOrderAmount:pObj Shopid:shopobj.shopID];
                NSString *num = [NSString stringWithFormat:@"已下订单 : %d",orderNumber];
                UILabel *count = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 30)];
                count.top = other.top;
                count.left = other.left-180;
                count.font = [UIFont systemFontOfSize:18];
                count.text = num;
                count.tag = 183000+i;
                [self addSubview:count];
                
                UIView *vv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, interval-20)];
                [v addSubview:vv];
                vv.center = l.center;
                vv.left = tmpSep.right + 20;
                vv.backgroundColor = tmpSep.backgroundColor;
                vv.tag = 500;
                int boarder = 2;
                UIImageView *pImageView = [[UIImageView alloc] initWithFrame:CGRectMake(boarder, boarder, vv.width-2*boarder, vv.height-boarder*2)];
                [vv addSubview:pImageView];
                //            pImageView.layer.borderColor = tmpSep.backgroundColor.CGColor;
                //            pImageView.layer.borderWidth = 2;
                pImageView.clipsToBounds = YES;
                pImageView.tag = 1000;
                pImageView.contentMode = UIViewContentModeScaleAspectFill;
                pImageView.backgroundColor = [UIColor blackColor];
                
                //            NSString *url = [NSString stringWithFormat:@"_%@",suit_.pic_path];
                //            path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:url];
                //            [imageCache_ imageForKey:pObj.pURL url:[NSURL URLWithString:path] queueIfNeeded:YES tag:i+100*suit_.n_id];
                 */
            }
        }
        else
        {
            img = [[UIHelper getInstance] getBundleImage:@"suit_line.png"];
            UIImageView *sep = [[UIImageView alloc] initWithImage:img];
            [self addSubview:sep];
            sep.top = imageview.top;
            sep.left = imageview.right + 25;
            self.userInteractionEnabled = YES;
            for (int i = 0; i < suit_.components.count; i++) {
                
                UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 530, interval)];
                [self addSubview:v];
                v.tag = 10 + i;
                //            v.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
                v.backgroundColor = [UIColor clearColor];
                v.left = sep.right + 25;
                v.top = imageview.top + interval*i;
                
    //            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    //            [v addGestureRecognizer:tap];
                [self addTapGesture:v];
                v.userInteractionEnabled = YES;
                
                SubSuitObject *obj = [suit_.components objectAtIndex:i];
                
                UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 30)];
                l.backgroundColor = [UIColor clearColor];
                l.font = [UIFont systemFontOfSize:24];
                [v addSubview:l];
                l.textColor = [UIColor blackColor];
                l.center = CGPointMake(0, v.height/2);
                l.left = 0;
                l.text = [NSString stringWithFormat:@"%@:",obj.subSuitType];
                
                ProductObject *pObj = [[DataManager sharedManager] getProductEx:obj.productCode withColorID:obj.colorid];
                UIView *tmpSep = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270, 2)];
                tmpSep.backgroundColor = [UIColor colorWithRed:158/255.0 green:121/255.0 blue:87/255 alpha:1];
                tmpSep.center = l.center;
                tmpSep.left = l.right-10;
                [v addSubview:tmpSep];
                
                UILabel *codeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 170, 30)];
                codeLabel.backgroundColor = [UIColor clearColor];
                codeLabel.font = [UIFont systemFontOfSize:22];
                [v addSubview:codeLabel];
                codeLabel.textColor = [UIColor blackColor];
                codeLabel.bottom = tmpSep.top;
                codeLabel.left = tmpSep.left-10;
                codeLabel.textAlignment = UITextAlignmentCenter;
                if (pObj && obj.productCode && obj.productCode.length>0) {
                    NSString *str = [NSString stringWithFormat:@"%@-%@",obj.productCode,pObj.pColor];
                    codeLabel.text = str;
                }else{
                    codeLabel.text = @"暂无";
                }
                codeLabel.adjustsFontSizeToFitWidth = YES;
                
                UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 30)];
                priceLabel.backgroundColor = [UIColor clearColor];
                priceLabel.font = [UIFont systemFontOfSize:22];
                [v addSubview:priceLabel];
                priceLabel.textColor = [UIColor blackColor];
                priceLabel.top = codeLabel.top;
                priceLabel.left = codeLabel.right+20;
                priceLabel.textAlignment = UITextAlignmentCenter;
                if (pObj && obj.productCode && obj.productCode.length>0) {
                    priceLabel.text = [NSString stringWithFormat:@"￥%.0f",pObj.pSale];
                }else{
                    priceLabel.text = @"暂无";
                }
                
                
                UIButton *other = [UIButton buttonWithType:UIButtonTypeCustom];
                other.frame = CGRectMake(0, 0, 50, 30);
                other.center = CGPointMake(680, 90);
                other.top = tmpSep.bottom + 50+interval*i;
                [other setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"anniu.png"] forState:UIControlStateNormal];
                [other setTitle:@"其他" forState:UIControlStateNormal];
                [other addTarget:self action:@selector(other:) forControlEvents:UIControlEventTouchUpInside];
                other.tag = 182000+i;
                [self addSubview:other];
                
                other.hidden = YES;
                
                ShopObject *shopobj=[[DataManager sharedManager]getCurrentShop];
                int orderNumber = [[DataManager sharedManager]getOrderAmount:pObj Shopid:shopobj.shopID];
                NSString *num = [NSString stringWithFormat:@"已下订单 : %d",orderNumber];
                UILabel *count = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 30)];
                count.top = other.top;
                count.left = other.left-180;
                count.font = [UIFont systemFontOfSize:18];
                count.text = num;
                count.tag = 183000+i;
                [self addSubview:count];
                
                UIView *vv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, interval-20)];
                [v addSubview:vv];
                vv.center = l.center;
                vv.left = tmpSep.right + 20;
                vv.backgroundColor = tmpSep.backgroundColor;
                vv.tag = 500;
                int boarder = 2;
                UIImageView *pImageView = [[UIImageView alloc] initWithFrame:CGRectMake(boarder, boarder, vv.width-2*boarder, vv.height-boarder*2)];
                [vv addSubview:pImageView];
    //            pImageView.layer.borderColor = tmpSep.backgroundColor.CGColor;
    //            pImageView.layer.borderWidth = 2;
                pImageView.clipsToBounds = YES;
                pImageView.tag = 1000;
                pImageView.contentMode = UIViewContentModeScaleAspectFill;
                pImageView.backgroundColor = [UIColor blackColor];
                
    //            NSString *url = [NSString stringWithFormat:@"_%@",suit_.pic_path];
    //            path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:url];
    //            [imageCache_ imageForKey:pObj.pURL url:[NSURL URLWithString:path] queueIfNeeded:YES tag:i+100*suit_.n_id];
            }
        }
#endif
    }
    return self;
}

- (void)addTapGesture:(UIView *)view{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [view addGestureRecognizer:tap];
    view.userInteractionEnabled = YES;
}

- (void)tap:(UITapGestureRecognizer *)gesuture{
    
    SubSuitObject *obj = [suit_.components objectAtIndex:gesuture.view.tag-10];
    ProductObject *pObj = [[DataManager sharedManager] getProduct:obj.productCode withColorID:obj.colorid];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"goToProductDetail2" object:pObj];
}

- (void)suit_image:(NSNotification *)notification{
    NSDictionary *dict = [notification userInfo];
    UIImage *img = [dict objectForKey:@"image"];
    int tag = [[dict objectForKey:@"tag"] intValue];
    
    int idx = tag / 100;
    if (idx != suit_.n_id) {
        return;
    }
    
    tag = tag%100;
    
    if (tag==99) {
        if (img) {
            if ([[PMGlobal getInstance] isImageEnable]) {
                suitImageView_.image = img;
            }
        }else{
            
        }
        
    }else{
        if ([[[PMGlobal getInstance]gettype]isEqualToString:@"type1"]) {
            ProductThumbView *v = (ProductThumbView *)[self viewWithTag:1001+tag];
            if ([[PMGlobal getInstance]isImageEnable]) {
                if (img) {
                    v.imageview.image = img;
                }else{
                    
                }
                
            }else{
                v.imageview.image = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];;
            }
        }
        else
        {
            UIView *v = [self viewWithTag:10+tag];
            UIView *vv = (UIView *)[v viewWithTag:500];
            UIImageView *imgV = (UIImageView *)[vv viewWithTag:1000];
            imgV.image = img;
        }
    }
}

#pragma mark 显示产品图片
- (void)product_detail_image:(NSNotification *)notification{
    NSDictionary *dict = [notification userInfo];
    UIImage *img = [dict objectForKey:@"image"];
    int tag = [[dict objectForKey:@"tag"] intValue];
    
    ProductThumbView *v = (ProductThumbView *)[self viewWithTag:tag];
    if ([[PMGlobal getInstance]isImageEnable]) {
        if (img) {
            v.imageview.image = img;
        }else{
            
        }
        
    }else{
        v.imageview.image = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];;
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)loadImage{
    NSString *url = [NSString stringWithFormat:@"_%@",suit_.pic_path];
    NSString *path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:url];
    UIImage *img = [imageCache_ imageForKey:url url:[NSURL URLWithString:path] queueIfNeeded:YES tag:99+100*suit_.n_id];
    
    if (img) {
        if ([[PMGlobal getInstance] isImageEnable]) {
        }else{
            img = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];;
        }
        
    }else{
        img = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];;
    }
    
    suitImageView_.image = img;
   // suitImageView_.image = [[UIHelper getInstance] getBundleImage:@"detail.png"];
    UIImage *img2 = [[UIHelper getInstance] getBundleImage:@"collect_image.png"];
    UIButton *collect = [UIButton buttonWithType:UIButtonTypeCustom];
    ShopObject *shopobj = [[DataManager sharedManager]getCurrentShop];
    if ([[DataManager sharedManager]productIssuitFavorite:suit_ Shopid:shopobj.shopID]>0) {
        [collect setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"collect_redimage.png"] forState:UIControlStateNormal];
    }
    else
    {
        [collect setBackgroundImage:img2 forState:UIControlStateNormal];
    }
    collect.backgroundColor = [UIColor darkGrayColor];
    collect.frame = CGRectMake(0, 0, img2.size.width, img2.size.height);
    
    // collect.right = productImageV.right-5;
    collect.top=10;
    collect.right = suitImageView_.right-40;
    collect.tag = 181000;
    [collect addTarget:self action:@selector(collect:) forControlEvents:UIControlEventTouchUpInside];
    [suitImageView_ addSubview:collect];
    
    for (int i = 0; i < suit_.components.count; i++) {
        UIView *v = [self viewWithTag:10+i];
        UIView *vv = (UIView *)[v viewWithTag:500];
        UIImageView *imgV = (UIImageView *)[vv viewWithTag:1000];
        
        SubSuitObject *obj = [suit_.components objectAtIndex:i];
//        ProductObject *pObj = [[DataManager sharedManager] getProduct:obj.productCode withColorID:obj.colorid];
        
        NSString *url1 = [NSString stringWithFormat:@"_%@",obj.productImage];
        NSString *path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:url1];
        UIImage *img = [imageCache_ imageForKey:url1 url:[NSURL URLWithString:path] queueIfNeeded:YES tag:i+100*suit_.n_id];
        if (img) {
            if ([[PMGlobal getInstance] isImageEnable]) {
                imgV.image = img;
            }else{
                imgV.image = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];;
            }
            
        }else{
            imgV.image = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];;
        }

    }
    
}

- (void)unloadImage{
    [imageCache_ cancelOperations];
    suitImageView_.image = nil;
    
    for (int i = 0; i < suit_.components.count; i++) {
        UIView *v = [self viewWithTag:10+i];
        UIView *vv = (UIView *)[v viewWithTag:100];
        UIImageView *imgV = (UIImageView *)[vv viewWithTag:1000];
        imgV.image = nil;
    }
}
- (void)other:(UIButton *)sender
{
    SubSuitObject *obj = [suit_.components objectAtIndex:sender.tag-182000];
    items1 = [NSMutableArray array];
    NSArray *item = [[DataManager sharedManager]GetSuitSimilarObject:suit_.scriptNo withType:obj.subSuitType];
    NSMutableArray *items2 = [NSMutableArray array];
    for (int i =0; i<item.count; i++) {
        SubSuitObject *sub = [item objectAtIndex:i];
        if (![sub.productCode isEqualToString:obj.productCode]) {
            NSString *str = [NSString stringWithFormat:@"%@-%@",suit_.c_mingc,sub.colorid];
            [items1 addObject:str];
            [items2 addObject:sub];
        }
    }
    controller = [[ShopViewController alloc] initWithStyle:UITableViewStylePlain items:items1 tag:104];
    controller.subobjs = items2;
    
    /*
     popup = [[PopupView alloc] initWithFrame:sender.frame withItems:items withHighlight:0 width:sender.left+35 tag:sender.tag];
     popup.delegate = self;
     [self.view addSubview:popup];
     */
    //the controller we want to present as a popover
    
    
    FPPopoverController *popover = [[FPPopoverController alloc] initWithViewController:controller];
    popover.distance = 52;
    //popover.arrowDirection = FPPopoverArrowDirectionAny;
    popover.tint = FPPopoverDefaultTint;
    
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        popover.contentSize = CGSizeMake(300, 500);
    }
    popover.arrowDirection = FPPopoverArrowDirectionUp;
    
    //sender is the UIButton view
    sender.left +=52;
    [popover presentPopoverFromView:sender];
    sender.left -=52;
}

- (void)click{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"suitImageClicked" object:suit_];
}
-(void)collect:(UIButton *)sender
{
    ShopObject *shopobj = [[DataManager sharedManager]getCurrentShop];
    if ([[DataManager sharedManager]productIssuitFavorite:suit_ Shopid:shopobj.shopID]==0) {
        [[DataManager sharedManager]setAssuitFavorite:suit_ withStar:1 Shopid:shopobj.shopID type:@"1"];
        [sender setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"collect_redimage.png"] forState:UIControlStateNormal];
    }
    else
    {
        [[DataManager sharedManager]cancelAssuitFavorite:suit_ Shopid:shopobj.shopID];
        [sender setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"collect_image.png"] forState:UIControlStateNormal];
    }
}

-(void)numreload:(NSNotification *)sender
{
    for (int i = 0; i < suit_.components.count; i++)
    {
        SubSuitObject *obj = [suit_.components objectAtIndex:i];
        ShopObject *shopobj=[[DataManager sharedManager]getCurrentShop];
//        ProductObject *pObj = [[DataManager sharedManager] getProduct:obj.productCode withColorID:obj.colorid];
        ProductObject *pObj = [[ProductObject alloc] init];
        pObj.pColorCode = obj.colorid;
        pObj.pCode = obj.productCode;
        int orderNumber = [[DataManager sharedManager]getOrderAmount:pObj Shopid:shopobj.shopID];
        UILabel *num = (UILabel *)[self viewWithTag:183000+i];
        num.text = [NSString stringWithFormat:@"%d",orderNumber];
    }
}
@end
